#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int var = 0;
void* thr(void* arg) {
    printf("子线程, id = [%ld], var = %d\n", pthread_self(), var);
    sleep(2);

    var = 1000;
    printf("子线程, id = [%ld], var = %d\n", pthread_self(), var);
}


int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr, NULL);
    if(ret != 0) {
        printf("pthread_create err: [%s]\n", strerror(ret));
    }

    pthread_detach(tid);
    printf("主线程, id = [%ld], var = %d\n", pthread_self(), var);
    
    var = 10086;
    sleep(5);
    printf("主线程, id = [%ld], var = %d\n", pthread_self(), var);
    
    ret = pthread_join(tid, NULL);
    if(ret != 0) {
        printf("pthread_join: [%s]\n", strerror(ret));
    }

    return 0;
}

